﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace FasterWPF
{
    public static class CanvasExt
    {
        //Due to order of precedence issues, only set Background Color via styles 
        //(so that we can apply MouseOverColor triggers)
        public static Canvas SetBackgroundColor(this Canvas chain, Brush newColor)
        {
            if (chain != null)
            {
                Style style = new Style(typeof(Canvas), chain.Style);

                Setter setter = new Setter();
                setter.Property = Canvas.BackgroundProperty;
                setter.Value = newColor;
                style.Setters.Add(setter);

                chain.Style = style;
            }

            return chain;
        }

        public static Canvas SetMouseOverColor(this Canvas parentCanvas, Brush brush)
        {
            Style style = new Style(typeof(Canvas), parentCanvas.Style);
            Trigger t = new Trigger();
            t.Property = Canvas.IsMouseOverProperty;
            t.Value = true;
            Setter setter = new Setter();
            setter.Property = Canvas.BackgroundProperty;
            setter.Value = brush;
            t.Setters.Add(setter);

            style.Triggers.Add(t); // Important: add a trigger before applying style
            parentCanvas.Style = style;

            return parentCanvas;
        }

        public static Canvas Initialize(this Canvas source, ContainerType containerType)
        {   
            //Force the Aero theme by default to assure the structure of the Listbox
            Uri uri = new Uri("/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml", UriKind.Relative);
            source.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);

            source.SetContainerType(containerType);
            IComposite icomp = CompositeTypeHelper.CreateIComposite<Canvas>(source);
            source.SetIComposite<Canvas>(icomp);

            //STORE SETTING BUILD CHAIN AND GET CHAIN?!?  Put these in a Composite implements an interface
            CanvasParent gbsr = new CanvasParent();
            source.StoreSetting<Canvas>("IParent", gbsr);

            source.AddItemBorder(Brushes.Gray, new Thickness(1, 1, 1, 1)); //default Border     
            source.StoreSetting<Canvas>("BorderSettings", new BorderSettings() { Width = double.NaN, Height = double.NaN, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(0, 0, 0, 0), CornerRadius = new CornerRadius(0), Padding = new Thickness(0) });

            return source;
        }
        
    }//end of class
}//end of namespace
